﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommerceSampleCode.CommercePaymentInstrument;
using Microsoft.Commerce.Proxy.PaymentInstrumentService;
using CommerceSampleCode.CommerceTestDataProvider;
using CommerceSampleCode;
using CommerceAccountProxy = Microsoft.Commerce.Proxy.AccountService;
using System.Xml;

namespace WebParts
{
    public partial class PIManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            test.Text = Request["accountID"];

            String CommercePaymentInstrumentUrl = (String)Application["CommercePaymentInstrumentUrl"];
            String CertLocation = (String)Application["Certlocation"];
            CommercePaymentInstrumentServiceAccessor paymentInstrumentService = new CommercePaymentInstrumentServiceAccessor(CommercePaymentInstrumentUrl, CertLocation);
            paymentInstrumentService.Initialize();

            String acountID = (String)Session["accountID"];
            CommerceAccountProxy.Identity requester = (CommerceAccountProxy.Identity)Session["requester"];
            CallerInfo piCallerInfo = CommercePaymentInstrumentDataProvider.CreateCallerInfo(requester, acountID);

            PaymentInstrument[] piInfos = paymentInstrumentService.GetPaymentInstruments(piCallerInfo, null);

            if (piInfos != null)
            {
                foreach (PaymentInstrument pi in piInfos)
                {
                    TableRow row = new TableRow();
                    PITable.Rows.Add(row);
                    TableCell cell = new TableCell();
                    cell.Text = pi.PaymentMethodID;
                    row.Cells.Add(cell);
                    cell = new TableCell();
                    cell.Text = pi.Type;
                    row.Cells.Add(cell);
                    cell = new TableCell();
                    cell.Text = pi.FriendlyName;
                    row.Cells.Add(cell);
                    if (pi.AddressInfo != null)
                    {
                        cell = new TableCell();
                        cell.Text = pi.AddressInfo.CountryCode;
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = pi.AddressInfo.State;
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = pi.AddressInfo.City;
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = pi.AddressInfo.Street1;
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = pi.AddressInfo.PostalCode;
                        row.Cells.Add(cell);
                    }
                    //if (pi.GetType() == typeof(CreditCardPaymentInstrument)) {
                        /*CreditCardPaymentInstrument creditCard = (CreditCardPaymentInstrument)pi;
                        cell = new TableCell();
                        cell.Text = creditCard.AccountHolderName;
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = creditCard.CardType;
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = creditCard.AccountNumberLastFour;
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = creditCard.AccountHolderName;
                        row.Cells.Add(cell);*/
                    //}

                    /*TableCell lastCell = new TableCell();
                    Button editButton = new Button();
                    editButton.Text = "Edit";
                    editButton.OnClientClick = "editLink_Click()";
                    lastCell.Controls.Add(editButton);
                    row.Cells.Add(lastCell);*/
                    
                }
            }
        }

        protected void editLink_Click(Object sender, EventArgs e)
        {
            //number.Text = "edit";
        }

        protected void addPI_Click(object sender, EventArgs e)
        {
            string locale = (string)Session["locale"];
            string ru = "http://localhost:7777/PIManagement.aspx";
            string accountID = (string)Session["accountID"];
            string url = String.Format("https://buy.live-int.com/PCS/uihost.cex?mkt={0}&partner=wldefault&flow=pcs&ru={1}&rx=https://buy.live-int.com/PCS/blank.htm&pitype={2}&piid=0&accountid={3}", locale, ru, "511",accountID);
            //Response.Write(url);
            Response.Redirect(url);
        }
    }
}